package com.company.huisuo.ext

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import com.company.huisuo.R
import com.company.huisuo.app.MyApp

fun String?.toast(duration: Int = Toast.LENGTH_SHORT) {
    if (this.isNullOrEmpty()) return
    val mToast = Toast.makeText(MyApp.context, this, duration)
    val inflate = MyApp.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    val view = inflate.inflate(R.layout.view_toast, null)
    view.layoutParams = LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT
    )
    mToast?.view = view
    mToast?.apply {
        val tvText = mToast.view?.findViewById<TextView>(R.id.tv_text)
        tvText?.text = this@toast
        setGravity(Gravity.CENTER, 0, 0)
        show()
    }
}